<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="二叉树 BinaryTree 1为根节点, 1是 2,3,4,5,6的父节点,相反他们是1的子节点,2,3,4,5,6是兄弟节点(父节点是同一个,并且在同一个层侧级别上) 一个树可以没有人和街店,成为空树 节点的度(degree): 1的度为5,因为他有5颗子树,2的度为2,61的度为0 树的度:           所有节点度中最大值,这棵树树的度为5 叶子节点:       度为0的节点,比如">
<meta property="og:type" content="article">
<meta property="og:title" content="BinaryTree">
<meta property="og:url" content="http://yoursite.com/2019/10/11/BinaryTree/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="二叉树 BinaryTree 1为根节点, 1是 2,3,4,5,6的父节点,相反他们是1的子节点,2,3,4,5,6是兄弟节点(父节点是同一个,并且在同一个层侧级别上) 一个树可以没有人和街店,成为空树 节点的度(degree): 1的度为5,因为他有5颗子树,2的度为2,61的度为0 树的度:           所有节点度中最大值,这棵树树的度为5 叶子节点:       度为0的节点,比如">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-012223.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-014949.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-015516.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-021444.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-074102.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-022418.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-033718.jpg">
<meta property="og:updated_time" content="2019-10-14T03:04:21.588Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="BinaryTree">
<meta name="twitter:description" content="二叉树 BinaryTree 1为根节点, 1是 2,3,4,5,6的父节点,相反他们是1的子节点,2,3,4,5,6是兄弟节点(父节点是同一个,并且在同一个层侧级别上) 一个树可以没有人和街店,成为空树 节点的度(degree): 1的度为5,因为他有5颗子树,2的度为2,61的度为0 树的度:           所有节点度中最大值,这棵树树的度为5 叶子节点:       度为0的节点,比如">
<meta name="twitter:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-012223.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/10/11/BinaryTree/"/>





  <title>BinaryTree | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/11/BinaryTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">BinaryTree</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-11T16:36:33+08:00">
                2019-10-11
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/11/BinaryTree/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/11/BinaryTree/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="二叉树-BinaryTree"><a href="#二叉树-BinaryTree" class="headerlink" title="二叉树 BinaryTree"></a>二叉树 BinaryTree</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-012223.png" alt=""></p>
<p><strong>1为根节点, 1是 2,3,4,5,6的父节点,相反他们是1的子节点,2,3,4,5,6是兄弟节点(父节点是同一个,并且在同一个层侧级别上)</strong></p>
<p><strong>一个树可以没有人和街店,成为空树</strong></p>
<p><strong>节点的度(degree): 1的度为5,因为他有5颗子树,2的度为2,61的度为0</strong></p>
<p><strong>树的度:           所有节点度中最大值,这棵树树的度为5</strong></p>
<p><strong>叶子节点:       度为0的节点,比如 4 21 221 222 223 31 51 52 61</strong></p>
<p><strong>非叶子节点:   度不是为0的节点为非叶子节点比如 2 3 5 6 22 </strong></p>
<p><strong>层数:              上面这棵树的层数为 4层,层数有些人是从0开始数</strong></p>
<p><strong>节点的深度(depth):         从根节点到当前节点的唯一路径上的节点总数,2的节点深度为2(经过了1和2),31的深度为3经过了1 3 31</strong></p>
<p><strong>节点的高度:       从当前节点到最远叶子节点的路径上的节点总数,2的节点高度为3</strong></p>
<p><strong>树的深度:          所有节点深度中的最大值,上面这棵树的深度为4</strong></p>
<p><strong>树的高度:          所有节点高度中的最大值 上面这棵树的高度为4</strong></p>
<p><strong>树的 深度 等于 树的 高度</strong></p>
<h3 id="有序树"><a href="#有序树" class="headerlink" title="有序树"></a>有序树</h3><p><strong>树中任意节点的子节点之间有顺序关系: 比如上面这棵树的第二层(仅第二层)</strong></p>
<h3 id="无序树"><a href="#无序树" class="headerlink" title="无序树"></a>无序树</h3><blockquote>
<p> <strong>树中任意节点的节点之间没有顺序关系,也称为自由树</strong></p>
</blockquote>
<h3 id="森林"><a href="#森林" class="headerlink" title="森林"></a>森林</h3><blockquote>
<p><strong>有m (m &gt;= 0) 课互不相交的树组成的集合: 也就是很多树接在一起层位森林</strong></p>
</blockquote>
<h3 id="二叉树Binary-Tree"><a href="#二叉树Binary-Tree" class="headerlink" title="二叉树Binary Tree"></a>二叉树Binary Tree</h3><blockquote>
<p><strong>每个节点的度最大为2(最多拥有 2 棵子树)</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-014949.png" alt=""></p>
<p><strong>即使某节点只有一棵树,也要区分左右子树,不能调换,所以有序树</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-015516.png" alt=""></p>
<p><strong>非空二叉树的第i层,最多有(2的 i-1次方)个节点 i &gt;= 1</strong></p>
<p><strong>2º = 1  2 的1次方= 2, 2 的平方= 4, 2的3次方 = 8</strong></p>
<p><strong>高度为h的二叉树上最多有 (2的 h次方-1) 个节点 (h &gt;= 1)</strong></p>
<p><strong>公式:    对于任何一棵非空二叉树,如果叶子节点个数为n0,度为2的节点数为n2,则 n0 = n2 + 1</strong></p>
<h3 id="真二叉树-Proper-Binary-Tree"><a href="#真二叉树-Proper-Binary-Tree" class="headerlink" title="真二叉树 Proper Binary Tree"></a>真二叉树 Proper Binary Tree</h3><hr>
<blockquote>
<p><strong>所有节点的度都要么为0,要么为2</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-021444.png" alt=""></p>
<h3 id="满二叉树-Full-Binary-Tree"><a href="#满二叉树-Full-Binary-Tree" class="headerlink" title="满二叉树 Full Binary Tree"></a>满二叉树 Full Binary Tree</h3><blockquote>
<p> <strong>所有节点的度都要么为0,要么为2,且所有的叶子节点都在最后一层</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-074102.png" alt=""></p>
<p><strong>满二叉树一定是真二叉树,真二叉树不一定是满二叉树</strong></p>
<p><strong>高度: log2(n + 1)</strong></p>
<h3 id="完全二叉树-Complete-Binary-Tree"><a href="#完全二叉树-Complete-Binary-Tree" class="headerlink" title="完全二叉树 Complete Binary Tree"></a>完全二叉树 Complete Binary Tree</h3><blockquote>
<p><strong>叶子节点只会出现在最后2层,且最后一层的叶子节点都靠左对齐,也就是从上到下,从左到右排序,度为1的节点只有左子树,度为1 的节点要么是1个要么是0个</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-022418.png" alt=""></p>
<p><strong>假设完全二叉树的高度为h(h &gt;= 1),那么至少有(2的h-1次方)个节点,最多 (2的h次方 - 1)(满二叉树的形态)</strong></p>
<p><strong>面试题&amp;&amp;公式</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-033718.jpg" alt=""></p>
<p><strong>题: 一个<code>完全!!!</code>二叉树有768个节点,求叶子节点的个数,也就是说 sum = 768 求出n0</strong></p>
<p><strong>总节点个数 n = n0 + n1 + n2 而且 n0 = n2 + 1</strong></p>
<p><strong>那么可以这样写n = n0 + n1 + n0 - 1, n = 2n0 + n1 - 1,从图或完全二叉树的规则上一直n1 要么是0要是1</strong></p>
<p><strong>n1为1时 总结点数n必然是偶数, n1为0时 总结点数必然是奇数</strong></p>
<p><strong>偶数时 会变成这样 n = 2n0,我们求的是n0 那么 n0 = n / 2,也就是 768 / 2那么非叶子节点也是 n/2</strong></p>
<p><strong>奇数时 会变成  n = 2n0 - 1,我们求的是n0 那么n0 = (n + 1) / 2, 非叶子节点 n - (n + 1) / 2变化 n - n / 2 - 1 / 2,在变化 n / 2 - 1 / 2 在变化 (n - 1) / 2 </strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">偶数时 	n0(叶子节点数量) = n / 2</span><br><span class="line">奇数		 n0(叶子节点数量) = (n + 1) / 2</span><br><span class="line">在变成的时候需要判断 n 是偶数还是奇数 需要 n % 2 == 0奇数这样,就觉得很麻烦</span><br><span class="line"></span><br><span class="line">变成一个公式</span><br><span class="line"></span><br><span class="line">假如就 (n + 1) / 2这个公式去算n0,那么奇数的时候是正确的(结果跟定是整数,不是浮点数),但是偶数的时候这个公式多了一个 1 / 2(结果肯定是浮点数),所以下取整数正好是n0,这样的话不管是偶数还是奇数都可以用这个公式</span><br><span class="line">向下取整数floor((n + 1) / 2),而编程中默认就是向下取整所以floor可以省略,就变成(n + 1) / 2</span><br><span class="line">又可以利用位运算变化成 (n + 1) &gt;&gt; 1 右移动1位</span><br><span class="line"></span><br><span class="line">反过来 假如就 n / 2 这个公式去算n0  那么就是向上取整数</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/07/19/动态数组/" rel="next" title="动态数组-02">
                <i class="fa fa-chevron-left"></i> 动态数组-02
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/10/12/SearchBinaryTree/" rel="prev" title="BinarySearchTree">
                BinarySearchTree <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#二叉树-BinaryTree"><span class="nav-number">1.</span> <span class="nav-text">二叉树 BinaryTree</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#有序树"><span class="nav-number">2.</span> <span class="nav-text">有序树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无序树"><span class="nav-number">3.</span> <span class="nav-text">无序树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#森林"><span class="nav-number">4.</span> <span class="nav-text">森林</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二叉树Binary-Tree"><span class="nav-number">5.</span> <span class="nav-text">二叉树Binary Tree</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#真二叉树-Proper-Binary-Tree"><span class="nav-number">6.</span> <span class="nav-text">真二叉树 Proper Binary Tree</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#满二叉树-Full-Binary-Tree"><span class="nav-number">7.</span> <span class="nav-text">满二叉树 Full Binary Tree</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#完全二叉树-Complete-Binary-Tree"><span class="nav-number">8.</span> <span class="nav-text">完全二叉树 Complete Binary Tree</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
